<?php

/**
 * @author
 * @copyright 2014
 */
class Klgb extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model();
        //$this->lang->load('title');
    }
    function _remap($method, $params = array()) {
        if (method_exists($this, $method)) {
            return call_user_func_array(array($this, $method), $params);
        }
        $this->_view();
    }
    function _view() {
		$data = new stdClass();
        $this->load->helper('form');
        $data->page = 0;
        $title = "Trung tâm"; 
		$data->cotrol = $this->uri->segment(1);
		
		$arr_lkl = $this->model->loaiketluan();
		$data->loaiketluans = form_dropdown('loaiklss', $arr_lkl,'', 'id="loaikls" style="width:158px; height:26px;" class=""');
		$chutris = $this->model->listChutri();
		$data->chutris = form_dropdown('chutriss', $chutris,'', 'id="chutris" style="width:158px; height:26px;" class=""');
		$nvchuyenquans = $this->model->listChuyenquan();
		$data->nvchuyenquans = form_dropdown('nvchuyenquanss', $nvchuyenquans,'', 'id="nvchuyenquans" style="width:158px; height:26px;" class=""');
		$phongbans = $this->model->listPhongban();
		$data->phongbans = form_dropdown('phongbanss', $phongbans,'', 'id="phongbans" style="width:158px; height:26px;" class=""'); 
		
        $content = $this->load->view('view', $data, true);
		$this->pub->write('title',$title,true);
		$this->pub->write('content',$content,true);
        $this->pub->render();
    }
	function getList(){
		 $data = new stdClass();
		 $result = new stdClass();
		 $page=$this->input->post('page');
         $counts=$this->input->post('count');
		 if(empty($counts)) $counts = $this->pub->config['numrows'];
		 $search = json_decode($this->input->post('search'),true);	
		 $sql = $this->model->getList($search);
		 $query = $this->model->query($sql)->execute();
		 $count  = $this->model->getTotal($search);
		 

		 $page_view='&nbsp;';
		 if($count>0){
			$page_view=$this->pub->phantrang($count,$counts,5,'ketluangiaoban/',$page);
		 }
         if(empty($page_view)) $page_view='&nbsp;';
         $result->paging = $page_view;
         $result->cPage = $page;
		 $data->datas = $query;
		 $data->count = $count;
         $result->content = $this->load->view('list',$data,true);
         $result->action = $this->pub->action;
		 echo json_encode($result);
	}
	function insert(){
		 $this->load->helper('form');
		 $data = new stdClass();
		 $arr_lkl = $this->model->loaiketluan();
		 $data->loaiketluans = form_dropdown('data[loaikl]', $arr_lkl,'', 'id="loaikls" style="width:158px; height:26px;" class=""');
		 
		 $chutris = $this->model->listChutri();
		 $data->chutris = form_dropdown('data[chutri]', $chutris,'', 'id="chutris" style="width:158px; height:26px;" class=""');
		 
		 $data->nguoiketluans = form_dropdown('data[nguoiketluan]', $chutris,'', 'id="nguoiketluan" style="width:158px; height:26px;" class=""');
		 
		 $nvchuyenquans = $this->model->listChuyenquan();
		 $data->nvchuyenquans = form_dropdown('data[nvchuyenquan]', $nvchuyenquans,'', 'id="nvchuyenquans" style="width:158px; height:26px;" class=""');
		 
		 $phongbans = $this->model->listPhongban();
		 $data->phongbans = form_dropdown('datas[phongban][]', $phongbans,'', 'id="phongbans" style="width:158px; height:26px;" class=""'); 
		
		 $loaigiaoban = $this->model->loaigiaoban();
		 $data->loaigiaobans = form_dropdown('data[loaigiaoban]', $loaigiaoban,'', 'id="loaigiaoban" style="width:158px; height:26px;" class=""');
		 	
		 $data->titles = "Thêm mới kết luận";
		 $content = $this->load->view('form', $data, true);
		 $this->pub->write('content',$content,true);
         $this->pub->render();	
	}
	function edit($ids){
		 $this->load->helper('form');
		 $data = new stdClass();
		 $id =  $this->input->post('id');	
		 $query = $this->model->where('id',$ids)->find();
		 
		 $data->ngaygiaoban = date('d-m-Y',strtotime($query->ngaygiaoban));
		 $data->ngayhoanthanh = date('d-m-Y',strtotime($query->ngayhoanthanh));
		 
		 $arr_lkl = $this->model->loaiketluan();
		 $data->loaiketluans = form_dropdown('data[loaikl]', $arr_lkl,$query->loaikl, 'id="loaikls" style="width:158px; height:26px;" class=""');
		 
		 $chutris = $this->model->listChutri();
		 $data->chutris = form_dropdown('data[]', $chutris,$query->chutri, 'id="chutris" style="width:158px; height:26px;" class=""');
		 
		 $data->nguoiketluans = form_dropdown('data[nguoiketluan]', $chutris,$query->nguoiketluan, 'id="nguoiketluan" style="width:158px; height:26px;" class=""');
		 
		 $nvchuyenquans = $this->model->listChuyenquan();
		 $data->nvchuyenquans = form_dropdown('data[nvchuyenquan]', $nvchuyenquans,$query->nvchuyenquan, 'id="nvchuyenquans" style="width:158px; height:26px;" class=""');
		 
		 $phongbans = $this->model->listPhongban();
		 $data->phongbans = form_dropdown('datas[phongban][]', $phongbans,'', 'id="phongbans" style="width:158px; height:26px;" class=""'); 
		
		 $loaigiaoban = $this->model->loaigiaoban();
		 $data->loaigiaobans = form_dropdown('data[loaigiaoban]', $loaigiaoban,'', 'id="loaigiaoban" style="width:158px; height:26px;" class=""');
		 $listPhongban = $this->model->getIDPhongban($ids);	
		 $str = "";
		 foreach($listPhongban as $id=>$nd){
			$str.= ",".$id;
		 }
		 $data->str = $str;
		 $data->datas = $query;
		 $data->ids = $ids;
		 $data->titles = "Sữa kết luận";
		 $content = $this->load->view('edit', $data, true);
		 $this->pub->write('content',$content,true);
         $this->pub->render();	
	}
	function save(){
		$str = $this->input->post('obj');
		$phongban = $this->input->post('phongban');
		$noidung = $this->input->post('noidung');
		$ghichu = $this->input->post('ghichu');
		$id = $this->input->post('id');		
		if($str !='{}'){
			$json = json_decode($str);
			$data = array();
			foreach($json as $clm=>$val){
				if($clm == 'ngaygiaoban' || $clm == 'ngayhoanthanh'){
					$val = date('Y-m-d',strtotime($val));
				}
				$data[$clm] = $val;
			}
			$data['noidung'] = $noidung;
			$data['ghichu'] = $ghichu;
			$result = $this->model->saves($data,$phongban,$id);
			echo $result;
		}
		else{
			echo 0;	
		}
	}
	function delete(){
		$str = $this->input->post('str');
		$item = $this->input->post('item');
		$this->model->deletes($str,$item);
	}
	function loadSearch(){
		$key = $this->input->post('key');
		$cartype = $this->model->where('isdelete',0)
							   ->select('typename')	
							   ->like('typename',$key) 
							   ->limit(10)
							   ->find_all();
		$str = "";
		foreach($cartype as $item){
			$str.= ','.($item->typename).'';
		}
		$str = substr($str,1);
		echo $str; 
	}
	function setDuyet(){
		$id = $this->input->post('id');
		$check = $this->input->post('check');
		$this->model->where('id',$id)->update(array('tpduyet'=>$check));	
	}
	function traKetqua(){
		 $this->load->helper('form');
		 $data = new stdClass();
		 $id =  $this->input->post('id');	
		 $query = $this->model->select('traketqua')->where('id',$id)->find();
		 $data->id = $id;
		 $data->traketqua = $query->traketqua;
		 echo $this->load->view('traketqua', $data, true);	
	}
	function luuTraKetqua(){
		 $id =  $this->input->post('id');
		 $kq =  $this->input->post('kq');	
		 $data = array();
		 $data['traketqua'] = $kq;
		 $query = $this->model->where('id',$id)->save($id,$data);
	}
}